package com.garen.fire.web;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.garen.common.BaseAction;
import com.garen.common.MsgBean;
import com.garen.fire.biz.IFireCasePhotoBiz;
import com.garen.sys.filter.SysFilter;

@Controller
@RequestMapping("/fire")
public class FireCasePhotoAction extends BaseAction {

	@Autowired
	private IFireCasePhotoBiz iFireCasePhotoBiz;

	/**
	 * 上传图片
	 * 
	 * @param ajbh
	 *            案件编号
	 * @param binaryStream
	 *            文件流
	 * @return
	 */
	@RequestMapping("/firecasephoto/upload")
	public ModelAndView upload(String ajbh, String binaryStream, String fileType) {
		MsgBean mb = SysFilter.getMsgBean();
		mb.setObj(iFireCasePhotoBiz.upload(ajbh, binaryStream,fileType));
		return Json(mb);
	}
	
	@RequestMapping("/firecasephoto/uploadImage")
	public ModelAndView uploadImage(@RequestParam MultipartFile file,HttpServletRequest request) {
		MsgBean mb = SysFilter.getMsgBean();
		String ajbh = request.getParameter("ajbh");
		String fileType = request.getParameter("fileType");
		mb.setObj(iFireCasePhotoBiz.upload(ajbh, file, fileType));
		return Json(mb);
	}
	
	
	
	

	/**
	 * 根据案件编号获取照片列表
	 * 
	 * @param ajbh
	 *            案件编号
	 * @return 照片列表
	 */
	@RequestMapping("/firecasephoto/list")
	public ModelAndView list(String ajbh) {
		MsgBean mb = SysFilter.getMsgBean();
		mb.setObj(iFireCasePhotoBiz.getFireCasePhotoListByAjbh(ajbh));
		return Json(mb);
	}

	
	/**
	 * 根据照片Id获取照片缩略图
	 * @param photoId 照片Id
	 * @return 照片缩略图
	 * @throws IOException
	 */
	@RequestMapping("/firecasephoto/preview")
	public ResponseEntity<byte[]> priview(int photoId) throws IOException {
		// 存放文件的路径
		String path = iFireCasePhotoBiz.getPreviewPathByPhotoId(photoId);
		String fileName = path.substring(path.lastIndexOf("/") + 1);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", fileName);
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File(path)), headers, HttpStatus.CREATED);
	}
	/**
	 * 根据照片Id获取照片
	 * @param photoId 照片Id
	 * @return 照片
	 * @throws IOException
	 */
	@RequestMapping("/firecasephoto/photo")
	public ResponseEntity<byte[]> photo(int photoId) throws IOException {
		// 存放文件的路径
		String path = iFireCasePhotoBiz.getPhotoPathByPhotoId(photoId);
		String fileName = path.substring(path.lastIndexOf("/") + 1);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", fileName);
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File(path)), headers, HttpStatus.CREATED);
	}
}
